#include <amxmodx>
new HDDs[250][32]
new ipgaged[64] = "../../gaged.cfg"
new logs[200][70]
new log_file[64] = "addons/amxmodx/logs/hddgaged.log"

public plugin_init()
{
	register_plugin("HDD gag","0.3","Mak-Di, Rekans")
	register_concmd("amx_gaghdd","cmdgag",ADMIN_LEVEL_A,"<hdd> - Gag this HDD")
	register_concmd("amx_gaglist","cmdgagl",ADMIN_LEVEL_A," - Show gaged hdd numbers")
	register_concmd("amx_ungaghdd","cmdungagip",ADMIN_LEVEL_A,"<ndd num>  - Ungaged hwID address")
	register_concmd("amx_gaglog","cmdgaglog",ADMIN_LEVEL_A," - Show log file")
	register_clcmd("say","check")
	register_clcmd("say_team","check")
	read_list()
}

public cmdgaglog(id)
{
	if (!(get_user_flags(id)&ADMIN_LEVEL_A)) return PLUGIN_CONTINUE
	
	read_logfile()
	client_print( id , print_notify , "------------- Log file -------------" )
	
	for(new i=0;i<200;i++)
	if(!equal(logs[i],"",0))
	{
		client_print( id , print_notify ,logs[i])
	}
	return PLUGIN_HANDLED
}

public cmdgag(id)
{
	if (!(get_user_flags(id)&ADMIN_LEVEL_A)) return PLUGIN_CONTINUE  
	
	new arg[32]
	read_argv(1, arg, 31)
	
	if(equal(arg,"",0))
	{
		client_print(id,print_notify,"Enter hdd number, please")
		return PLUGIN_HANDLED
	}
	
	for(new i=0;i<250;i++)
	if(equal(HDDs[i],arg,0))
	{
		client_print(id,print_notify,"This hdd already gaged")
		return PLUGIN_HANDLED
	}
	
	for(new i=0;i<250;i++)
	if(equal(HDDs[i],"",0))
	{
		HDDs[i]=arg
		client_print(id,print_notify,"HDD: %s was gaged",arg)
		add_log(id,arg)
		delete_file(ipgaged)
		for(new j=0;j<250;j++)
		
		if(!equal(HDDs[j],"",0))
		{
			
			write_file(ipgaged,HDDs[j],-1)
		}
		
		return PLUGIN_HANDLED
	}
	return PLUGIN_HANDLED
}

public cmdgagl(id)
{
	if (!(get_user_flags(id)&ADMIN_LEVEL_A)) return PLUGIN_CONTINUE
	client_print( id , print_notify , "------------- Gaged HDD numbers -------------" )
	
	for(new i=0;i<250;i++)
	if(!equal(HDDs[i],"",0))
	{
		client_print( id , print_notify ,HDDs[i])
	}
	
	return PLUGIN_HANDLED
}

public cmdungagip(id)
{
	if (!(get_user_flags(id)&ADMIN_LEVEL_A)) return PLUGIN_CONTINUE
	new arg[32]
	read_argv(1, arg, 31)
	
	if(equal(arg,"",0))
	{
		client_print(id,print_notify,"Enter hdd number, please")
		return PLUGIN_HANDLED
	}
	
	for(new i=0;i<250;i++)
	if(equal(HDDs[i],arg,0))
	{
		client_print( id , print_notify ,"HDD: %s was removed",arg)
		HDDs[i]=""
		rm_log(id,arg)
		delete_file(ipgaged)
		for(new j=0;j<250;j++)
		if(!equal(HDDs[j],"",0))
		{
			write_file(ipgaged,HDDs[j],-1)
		}
		return PLUGIN_HANDLED
	}
	client_print( id , print_notify ,"HDD: %s not found",arg)
	return PLUGIN_HANDLED
}

public check(id)
{
	new userhdd[32]
	
	//get_user_ip(id,userip,32,-1)
	get_user_info(id, "*hwID", userhdd, 32)
	
	for(new i=0;i<250;i++)
	if(containi(userhdd,HDDs[i])!=-1)
	{
		client_print( id , print_chat ,"Sorry, you can't speak")
		return PLUGIN_HANDLED
	}
	
	return PLUGIN_CONTINUE
}

public read_list()
{
	new index=0, line[32]
	if (file_exists(ipgaged))
	{
		new length
		new i
		while(read_file(ipgaged,index,line,32,length))
		{
			for(i = 0; line[i]; i++) 
				if(line[i] == '^r' || line[i] =='^n') line[i] = 0
				
			if(line[0]) HDDs[index++] = line
		}
	}
	else log_message("[AMXX] hddgaged file not found")
	
	return PLUGIN_CONTINUE
}

stock add_log(id,const ip[])
{
	new uname[31],Info[70],Time[25]
	get_user_name(id,uname,31)
	get_time("%m/%d/%Y - %H:%M",Time,25)
	format(Info,70,"Admin %s added hdd: %s %s",uname,ip,Time)
	write_file(log_file,Info,-1)
	return PLUGIN_CONTINUE
}

stock rm_log(id,const ip[])
{
	new uname[31],Info[70],Time[25]
	get_user_name(id,uname,31)
	get_time("%m/%d/%Y - %H:%M",Time,25)
	format(Info,70,"Admin %s removed hdd: %s %s",uname,ip,Time)
	write_file(log_file,Info,-1)
	return PLUGIN_CONTINUE
}

public read_logfile()
{
	new index=0, line[70]
	if (file_exists(log_file))
	{
		new length
		new i
		while(read_file(log_file,index,line,70,length)){
			for(i = 0; line[i]; i++) if(line[i] == '^r' || line[i] =='^n') line[i] = 0
			if(line[0]) logs[index++] = line
		}
	}
	else log_message("[AMXX] log file not found")
	
	return PLUGIN_CONTINUE
}
